<?php
/**
 * Created by PhpStorm.
 * User: lee
 *
 * 功能：处理队列中待退的资产
 * 版本：v1.0
 *
 *
 * 规则：1分钟拉取一次待处理数据
 *
 *
 *
 */

ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\trade\OrderRefund as mOrderRefund;
use Biz\model\trade\OrderRefundQueue as mOrderRefundQueue;
use Biz\service\trade\OrderRefund as sOrderRefund;
use Biz\module\Classes\OrderRefundQueue as clsOrderRefundQueue;


$where = [
    'OR' => [
        //'refund_integral_status'          => mOrderRefundQueue::REFUND_INTEGRAL_STATUS_NORMAL,
        'refund_rmb_status'               => mOrderRefundQueue::REFUND_RMB_STATUS_NORMAL,
        //'refund_commission_amount_status' => mOrderRefundQueue::REFUND_COMMISSION_STATUS_NORMAL
    ]
];

try {
    //取出待处理的数据
    $refundQueueList = mOrderRefundQueue::select("*", $where);

    if (empty($refundQueueList)) {
        throw new \Exception("当前时刻没有待处理的队列数据");
    }

    $clsOrderRefundQueue = new clsOrderRefundQueue();

    foreach ($refundQueueList as $refundQueue) {

        try {

            if ($refundQueue['refund_rmb_status'] == mOrderRefundQueue::REFUND_RMB_STATUS_NORMAL) {
                $clsOrderRefundQueue->refundRmb($refundQueue['id'],$refundQueue['is_sys'],$refundQueue['shop_id'],$refundQueue['uid'],$refundQueue['order_no'],$refundQueue['refund_order_no'],$refundQueue['is_confirm_receipt'],$refundQueue['refund_rmb'],$refundQueue['refund_shipping_amount'],$refundQueue['refund_integral_amount'],$refundQueue['refund_commission_amount']);
            }else{
                continue;
            }

        } catch (\Exception $e) {

            echo sprintf("%s    refundQueueId:%s    queueData:%s 异常：%s  \n", date('Y-m-d H:i:s'), $refundQueue['id'],print_r($refundQueue,true), json_encode([
                'e_file' => $e->getFile(),
                'e_line' => $e->getLine(),
                'e_msg'  => $e->getMessage()
            ], JSON_UNESCAPED_UNICODE));

        } finally {
            echo sprintf("%s    refundQueueId:%s    sql日志：%s   \n", date('Y-m-d H:i:s'),$refundQueue['id'], mOrderRefund::sqlLogs());

            mOrderRefund::clearLogs();
        }
    }
} catch (\Exception $e) {

    echo sprintf("%s    %s\n", date('Y-m-d H:i:s'), $e->getMessage());

}






